﻿///<reference path="canvas.ts" />
///<reference path="spritebatcher.ts" />
///<reference path="resmgr.ts" />
namespace lighttool.egret
{
    class AdapterGeom extends egret3d.Geometry
    {
        canvas: spriteCanvas;
        useraction: lighttool.canvasAction;
        mesh: egret3d.Mesh;
        meshpick: egret3d.Mesh;
        camera: egret3d.Camera3D;
        public buildDefaultSubGeometry()
        {

        }
        public activeState(time: number, delay: number, context3DProxy: egret3d.Context3DProxy, camera3D: egret3d.Camera3D)
        {
            var vp = this.camera.viewProjectionMatrix.clone();
            //var vp = camera.getViewMatrix().multiply(camera._projectionMatrix);
            //var m = node._worldMatrix.clone();
            var m = this.mesh.modelMatrix.clone();
            var cl = new egret3d.Matrix4_4();
            let asp = this.canvas.width / this.canvas.height;

            var size = 1;
            cl.rawData = new Float32Array([
                size * 1 / this.canvas.width, 0, 0, 0,//去掉asp的影响
                0, 0, size * -1 * 1 / this.canvas.height, 0,
                0, 1, 0, 0,
                -0.5 * size, 0, 0.5 * size, 1
            ]);
            var mvp = cl.clone();
            mvp.multiply(m);//
            //m = cl.multiply(m); //加上我们的canvas调整
            mvp.multiply(vp);
            //var mvp = m.multiply(vp);
            //calc matrix here.
            this.canvas.spriteBatcher.matrix = mvp.rawData;

            //自动缩放
            this.meshpick.scaleX = this.meshpick.scaleY * asp;
            //node.scaling.x = node.scaling.y * asp;


            this.canvas.spriteBatcher.begindraw();

            this.useraction.ondraw(this.canvas);

            this.canvas.spriteBatcher.enddraw();
        }

    }

    export class canvasAdapter
    {
        static _calcRootPos(e: HTMLElement): { x: number, y: number }
        {
            var divf: HTMLElement = e;
            var left = 0;
            var top = 0;
            while (divf != null)
            {
                left += divf.offsetLeft;
                top += divf.offsetTop;
                divf = divf.parentElement as HTMLElement;
            }
            return { x: left, y: top };
        }
        static CreateScreenCanvas(egretCanvas: egret3d.Egret3DCanvas, useraction: lighttool.canvasAction)
        {
            var webgl = egret3d.Context3DProxy.gl;
            var c = new spriteCanvas(webgl, webgl.drawingBufferWidth, webgl.drawingBufferHeight);
            //var asp = range.width / range.height;
            c.spriteBatcher.matrix = new Float32Array([
                1.0 * 2 / egretCanvas.width, 0, 0, 0,//去掉asp的影响
                0, 1 * -1 * 2 / egretCanvas.height, 0, 0,
                0, 0, 1, 0,
                -1, 1, 0, 1
            ]);
            c.spriteBatcher.ztest = false;//最前不需要ztest

            var ua = useraction;

            egretCanvas.afterRender = () =>
            {
                //改成状态检查形式
                if (c.width != egretCanvas.width || c.height != egretCanvas.height)
                {
                    c.width = egretCanvas.width;
                    c.height = egretCanvas.height;
                    c.spriteBatcher.matrix = new Float32Array([
                        1.0 * 2 / egretCanvas.width, 0, 0, 0,//去掉asp的影响
                        0, 1 * -1 * 2 / egretCanvas.height, 0, 0,
                        0, 0, 1, 0,
                        -1, 1, 0, 1
                    ]);
                    ua.onresize(c);
                }
                c.spriteBatcher.begindraw();

                ua.ondraw(c);

                c.spriteBatcher.enddraw();
            };

            //煤球用了，不会触发
            //egret3d.Input.addEventListener(egret3d.Event3D.RESIZE, () =>
            //{
            //    c.width = window.innerWidth;
            //    c.height = window.innerHeight;
            //    ua.onresize(c);
            //}, null);
            var inputlast: HTMLInputElement = null;
            document.addEventListener("touchstart", (ev) =>
            {
                if (inputlast != null)
                {
                    inputlast.blur();
                }
                if (ev.target instanceof HTMLInputElement)
                {
                    inputlast = ev.target as HTMLInputElement;
                    inputlast.focus();
                    ev.preventDefault();
                }

                var p = canvasAdapter._calcRootPos(webgl.canvas);
                var s = 1.0;
                var skip: boolean = ua.onpointevent(c, canvaspointevent.POINT_DOWN, (ev.touches[0].clientX - p.x) * s, (ev.touches[0].clientY - p.y) * s);
                if (skip)
                    ev.preventDefault();
            }
            );
            document.addEventListener("mousedown", (ev) =>
            {
                var p = canvasAdapter._calcRootPos(webgl.canvas);
                var s = 1.0;
                var skip: boolean = ua.onpointevent(c, canvaspointevent.POINT_DOWN, (ev.clientX - p.x) * s, (ev.clientY - p.y) * s);
                if (skip)
                    ev.preventDefault();
            }
            );
            document.addEventListener("touchend", (ev) =>
            {
                var skip: boolean = ua.onpointevent(c, canvaspointevent.POINT_UP, 0, 0);
                if (skip)
                    ev.preventDefault();
            }
            );
            document.addEventListener("touchcancel", (ev) =>
            {
                var skip: boolean = ua.onpointevent(c, canvaspointevent.POINT_UP, 0, 0);
                if (skip)
                    ev.preventDefault();
            }
            );
            document.addEventListener("mouseup", (ev) =>
            {
                //需要注意POINTUP时间的 x ，y 没有价值
                var skip: boolean = ua.onpointevent(c, canvaspointevent.POINT_UP, 0, 0);
                if (skip)
                    ev.preventDefault();
            }
            );
            document.addEventListener("touchmove", (ev) =>
            {
                var p = canvasAdapter._calcRootPos(webgl.canvas);
                var s = 1.0;
                var skip: boolean = ua.onpointevent(c, canvaspointevent.POINT_MOVE, (ev.touches[0].clientX - p.x) * s, (ev.touches[0].clientY - p.y) * s);
                if (skip)
                    ev.preventDefault();
            }
            );
            document.addEventListener("mousemove", (ev) =>
            {
                var p = canvasAdapter._calcRootPos(webgl.canvas);
                var s = 1.0;
                var skip: boolean = ua.onpointevent(c, canvaspointevent.POINT_MOVE, (ev.clientX - p.x) * s, (ev.clientY - p.y) * s);
                if (skip)
                    ev.preventDefault();
            }
            );
        }

        static fixheight_ScreenCanvas: number = 400;
        static CreateScreenCanvasFixHeight(egretCanvas: egret3d.Egret3DCanvas, useraction: lighttool.canvasAction)
        {
            var webgl = egret3d.Context3DProxy.gl;
            var c = new spriteCanvas(webgl, webgl.drawingBufferWidth, webgl.drawingBufferHeight);
            //var asp = range.width / range.height;
            var asp = egretCanvas.width / egretCanvas.height;

            c.spriteBatcher.matrix = new Float32Array([
                1.0 * 2 / (canvasAdapter.fixheight_ScreenCanvas * asp), 0, 0, 0,//去掉asp的影响
                0, 1 * -1 * 2 / canvasAdapter.fixheight_ScreenCanvas, 0, 0,
                0, 0, 1, 0,
                -1, 1, 0, 1
            ]);
            c.spriteBatcher.ztest = false;//最前不需要ztest

            var ua = useraction;
            var nowheight = canvasAdapter.fixheight_ScreenCanvas;
            egretCanvas.afterRender = () =>
            {
                var asp = egretCanvas.width / egretCanvas.height;
                var fw = (canvasAdapter.fixheight_ScreenCanvas * asp) | 0;
                //改成状态检查形式
                if (c.width != fw || c.height != canvasAdapter.fixheight_ScreenCanvas || nowheight != c.height)
                {
                    nowheight = canvasAdapter.fixheight_ScreenCanvas;
                    c.width = fw;
                    c.height = canvasAdapter.fixheight_ScreenCanvas;

                    c.spriteBatcher.matrix = new Float32Array([
                        1.0 * 2 / c.width, 0, 0, 0,//去掉asp的影响
                        0, 1 * -1 * 2 / c.height, 0, 0,
                        0, 0, 1, 0,
                        -1, 1, 0, 1
                    ]);
                    ua.onresize(c);
                }
                c.spriteBatcher.begindraw();

                ua.ondraw(c);

                c.spriteBatcher.enddraw();
            };

            //煤球用了，不会触发
            //egret3d.Input.addEventListener(egret3d.Event3D.RESIZE, () =>
            //{
            //    c.width = window.innerWidth;
            //    c.height = window.innerHeight;
            //    ua.onresize(c);
            //}, null);

            var inputlast: HTMLInputElement = null;
            document.addEventListener("touchstart", (ev) =>
            {
                if (inputlast != null)
                {
                    inputlast.blur();
                }
                if (ev.target instanceof HTMLInputElement)
                {
                    inputlast = ev.target as HTMLInputElement;
                    inputlast.focus();
                    ev.preventDefault();
                }

                var p = canvasAdapter._calcRootPos(webgl.canvas);
                var s = canvasAdapter.fixheight_ScreenCanvas / egretCanvas.height;
                var skip: boolean = ua.onpointevent(c, canvaspointevent.POINT_DOWN, (ev.touches[0].clientX - p.x) * s, (ev.touches[0].clientY - p.y) * s);
                if (skip)
                    ev.preventDefault();
            }
            );
            document.addEventListener("mousedown", (ev) =>
            {
                var p = canvasAdapter._calcRootPos(webgl.canvas);
                var s = canvasAdapter.fixheight_ScreenCanvas / egretCanvas.height;
                var skip: boolean = ua.onpointevent(c, canvaspointevent.POINT_DOWN, (ev.clientX - p.x) * s, (ev.clientY - p.y) * s);
                if (skip)
                    ev.preventDefault();
            }
            );
            document.addEventListener("touchend", (ev) =>
            {
                var skip: boolean = ua.onpointevent(c, canvaspointevent.POINT_UP, 0, 0);
                if (skip)
                    ev.preventDefault();
            }
            );
            document.addEventListener("touchcancel", (ev) =>
            {
                var skip: boolean = ua.onpointevent(c, canvaspointevent.POINT_UP, 0, 0);
                if (skip)
                    ev.preventDefault();
            }
            );
            document.addEventListener("mouseup", (ev) =>
            {
                //需要注意POINTUP时间的 x ，y 没有价值
                var skip: boolean = ua.onpointevent(c, canvaspointevent.POINT_UP, 0, 0);
                if (skip)
                    ev.preventDefault();
            }
            );
            document.addEventListener("touchmove", (ev) =>
            {
                var p = canvasAdapter._calcRootPos(webgl.canvas);
                var s = canvasAdapter.fixheight_ScreenCanvas / egretCanvas.height;
                var skip: boolean = ua.onpointevent(c, canvaspointevent.POINT_MOVE, (ev.touches[0].clientX - p.x) * s, (ev.touches[0].clientY - p.y) * s);
                if (skip)
                    ev.preventDefault();
            }
            );
            document.addEventListener("mousemove", (ev) =>
            {
                var p = canvasAdapter._calcRootPos(webgl.canvas);
                var s = canvasAdapter.fixheight_ScreenCanvas / egretCanvas.height;
                var skip: boolean = ua.onpointevent(c, canvaspointevent.POINT_MOVE, (ev.clientX - p.x) * s, (ev.clientY - p.y) * s);
                if (skip)
                    ev.preventDefault();
            }
            );

            //egret3d.Input.addEventListener(egret3d.MouseEvent3D.MOUSE_MOVE, (e: egret3d.MouseEvent3D) =>
            //{
            //    var p = canvasAdapter._calcRootPos(webgl.canvas);
            //    var s = canvasAdapter.fixheight_ScreenCanvas / egretCanvas.height;

            //    var skip: boolean = ua.onpointevent(c, canvaspointevent.POINT_MOVE, (egret3d.Input.mouseX - p.x) * s, (egret3d.Input.mouseY - p.y) * s);
            //    //返回值表示是否要截断事件
            //    //egret 事件设计中没有对应概念 
            //}, null);

            //egret3d.Input.addEventListener(egret3d.MouseEvent3D.MOUSE_DOWN, (e: egret3d.MouseEvent3D) =>
            //{
            //    var p = canvasAdapter._calcRootPos(webgl.canvas);
            //    var s = canvasAdapter.fixheight_ScreenCanvas / egretCanvas.height;

            //    ua.onpointevent(c, canvaspointevent.POINT_DOWN, (egret3d.Input.mouseX - p.x) * s, (egret3d.Input.mouseY - p.y) * s);
            //}, null);

            //egret3d.Input.addEventListener(egret3d.MouseEvent3D.MOUSE_UP, (e: egret3d.MouseEvent3D) =>
            //{
            //    var p = canvasAdapter._calcRootPos(webgl.canvas);
            //    var s = canvasAdapter.fixheight_ScreenCanvas / egretCanvas.height;

            //    ua.onpointevent(c, canvaspointevent.POINT_UP, (egret3d.Input.mouseX - p.x) * s, (egret3d.Input.mouseY - p.y) * s);
            //}, null);
        }


        static Create3DCanvas(egretView: egret3d.View3D, canvasWidth: number, canvasHeight: number, useraction: lighttool.canvasAction)
        {
            alert("Create3DCanvas");
            var webgl = egret3d.Context3DProxy.gl;
            var c = new spriteCanvas(webgl, canvasWidth, canvasHeight);

            //var asp = range.width / range.height;
            c.spriteBatcher.matrix = new Float32Array([
                1.0 * 2 / c.width, 0, 0, 0,//去掉asp的影响
                0, 1 * -1 * 2 / c.height, 0, 0,
                0, 0, 1, 0,
                -1, 1, 0, 1
            ]);
            c.spriteBatcher.ztest = false//最前不需要ztest


            var geom2 = new egret3d.PlaneGeometry(1, 1);
            var mat: egret3d.ColorMaterial = new egret3d.ColorMaterial();
            mat.alpha = 0;
            var planepick = new egret3d.Mesh(geom2, mat);
            egretView.addChild3D(planepick);


            var geom = new AdapterGeom();
            geom.canvas = c;
            geom.useraction = useraction;
            geom.camera = egretView.camera3D;
            var plane = new egret3d.Mesh(geom, mat);
            planepick.scaleX = planepick.scaleY = planepick.scaleZ = 100;
            geom.mesh = plane;
            geom.meshpick = planepick;
            planepick.addChild(plane);

            planepick.enablePick = true;
            planepick.addEventListener(egret3d.PickEvent3D.PICK_MOVE, (e: egret3d.PickEvent3D) =>
            {
                var x = (0.5 + e.pickResult.localPosition.x) * c.width;
                var y = (0.5 - e.pickResult.localPosition.z) * c.height;
                var skip = useraction.onpointevent(c, canvaspointevent.POINT_MOVE, x, y);

            }, null);
            planepick.addEventListener(egret3d.PickEvent3D.PICK_DOWN, (e: egret3d.PickEvent3D) =>
            {
                var x = (0.5 + e.pickResult.localPosition.x) * c.width;
                var y = (0.5 - e.pickResult.localPosition.z) * c.height;
                var skip = useraction.onpointevent(c, canvaspointevent.POINT_DOWN, x, y);

            }, null);
            planepick.addEventListener(egret3d.PickEvent3D.PICK_UP, (e: egret3d.PickEvent3D) =>
            {
                var x = (0.5 + e.pickResult.localPosition.x) * c.width;
                var y = (0.5 - e.pickResult.localPosition.z) * c.height;
                var skip = useraction.onpointevent(c, canvaspointevent.POINT_UP, x, y);

            }, null);

            return plane;
        }
    }
}